package com.veo.dao.Impl;

import com.veo.dao.BookManageDao;
import com.veo.entity.Books;
import com.veo.entity.BorrowBooks;
import com.veo.entity.User;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository("bookManageDao")
public class BookManageDaoImpl extends HibernateDaoSupport implements BookManageDao {

    @Resource(name = "sessionFactory")
    public void setSessionFactory1(SessionFactory sessionFactory){
        super.setSessionFactory(sessionFactory);
    }



    @Override
    public List<BorrowBooks> getAllBorrowBooks() {
        SessionFactory sessionFactory = this.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        String sql="select isbn,books.bookName,author,type,press,idCard,user.username,borrowTime from borrowBooks,books,user\n" +
                "                where books.id=borrowBooks.bookId and user.id=borrowBooks.userId";
        List list = session.createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(BorrowBooks.class)).list();
        return list;
    }

    @Override
    public boolean findIsbn(Books books) {
        List<Books> list = (List<Books>) getHibernateTemplate().find("from Books where isbn='" + books.getIsbn()+"'");
        if (list.size()>0){
            return true;
        }
        return false;
    }

    @Override
    public void addBorrowBooks(String userId, String bookId) {
        SessionFactory sessionFactory = this.getSessionFactory();
        assert sessionFactory != null;
        Session session = sessionFactory.getCurrentSession();
        String sql="insert into borrowbooks (userId,bookId) values(?,?)";
        Query query = session.createSQLQuery(sql);
        query.setInteger(0,Integer.parseInt(userId));
        query.setInteger(1,Integer.parseInt(bookId));
        query.executeUpdate();
    }

    @Override
    public void deleteBorrowBooks(String userId, String bookId) {
        SessionFactory sessionFactory = this.getSessionFactory();
        assert sessionFactory != null;
        Session session = sessionFactory.getCurrentSession();
        String sql="delete from borrowbooks where userId=? and bookId=?";
        Query query = session.createSQLQuery(sql);
        query.setInteger(0,Integer.parseInt(userId));
        query.setInteger(1,Integer.parseInt(bookId));
        query.executeUpdate();
    }

    @Override
    public User findUserId(String idCard) {
        List<User> list = (List<User>) getHibernateTemplate().find("from User where idCard='" + idCard+"'");
        if (list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public Books findBookId(String isbn) {
        List<Books> list = (List<Books>) getHibernateTemplate().find("from Books where isbn='" + isbn+"'");
        if (list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public boolean findBorrowBooks(String userId, String bookId) {
        SessionFactory sessionFactory = this.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        String sql="select * from borrowbooks where userId=? and bookId=?";
        SQLQuery sqlQuery = session.createSQLQuery(sql);
        sqlQuery.setInteger(0,Integer.parseInt(userId));
        sqlQuery.setInteger(1,Integer.parseInt(bookId));
        List list = sqlQuery.list();
        if (list.size()>0){
            return true;
        }
        return false;
    }
}
